package gin

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-source-learn/src/advance/gin/error"
	"net/http"
)

// router
func router() {
	r := gin.Default()

	r.GET("/get", handle)

	r.Run(":8080")
}

func handle(c *gin.Context) {
	c.String(http.StatusOK, "Gin get请求！")
}

func router_return_json() {
	r := gin.Default()

	r.GET("/json", handle_return_price)

	r.Run(":8081")

}

func handle_return_price(c *gin.Context) {
	data := map[string]interface{}{
		"name": "prince",
		"age":  18,
	}
	c.JSON(http.StatusOK, data)
}

func Users(c *gin.Context) {
	c.AbortWithError(200, error.ServerError)
}

func User(c *gin.Context) {
	c.AbortWithError(200, errors.New("this is error"))
}

func Ping(c *gin.Context) {
	c.JSON(200, gin.H{
		"message": "success",
	})
}

func OnStart() {
	r := gin.Default()
	r.NoMethod(error.HandleNotFound)
	r.NoRoute(error.HandleNotFound)
	r.Use(error.ErrHandler())

	r.GET("/user", User)
	r.GET("/users", Users)
	r.GET("/", Ping)
	r.Run(":8090") // 在 0.0.0.0:8080 上监听并服务
}
